//
//  MemSetViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/3/31.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "MemSetViewC.h"
#import "AboutUSViewC.h"
#import <UMFeedback.h>
#import <UMessage.h>
#import "Util.h"
#import <MBProgressHUD.h>

@interface MemSetViewC (){
    NSString *num;
}

@end

@implementation MemSetViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title =@"设置";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    
    NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        num = [NSString stringWithFormat:@"%lluM",[[manager attributesOfItemAtPath:filePath error:nil] fileSize]];
    }
    else{
        num = @"0M";
    }
    
    _cachLab.text = num;
    
    [_imforSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)switchAction:(id)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    if (isButtonOn) {
        if (IOS_8_OR_LATER) {
            //register remoteNotification types
            UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
            action1.identifier = @"action1_identifier";
            action1.title=@"Accept";
            action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
            
            UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
            action2.identifier = @"action2_identifier";
            action2.title=@"Reject";
            action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序，在后台处理
            action2.authenticationRequired = YES;//需要解锁才能处理，如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略；
            action2.destructive = YES;
            
            UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
            categorys.identifier = @"category1";//这组动作的唯一标示
            [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
            
            UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
                                                                                         categories:[NSSet setWithObject:categorys]];
            
            [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];
        } else {
            [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |
             UIRemoteNotificationTypeSound |
             UIRemoteNotificationTypeAlert];
        }

        
    }else {
        [UMessage unregisterForRemoteNotifications];
    }


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 1) {
        NSString *number = @"tel://0519-81181000";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
    }
    if (indexPath.row == 2) {
        [self.navigationController pushViewController:[UMFeedback feedbackViewController]
                                             animated:YES];
    }
    if (indexPath.row == 3) {
        UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                       bundle:nil];
        AboutUSViewC *abovc = [main instantiateViewControllerWithIdentifier:@"AboutUSViewC"];
        [self.navigationController pushViewController:abovc animated:YES];
        
    }
    if (indexPath.row == 4) {
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        CFShow((__bridge CFTypeRef)(infoDic));
        
        NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
        if ([appVersion isEqualToString:@"1"]) {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            HUD.labelText = @"当前已是最新版本";
            [HUD hide:YES afterDelay:1.5f];
        }
        else {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            HUD.labelText = @"已检测到新版本需要更新";
            [HUD hide:YES afterDelay:1.5f];
        }
        
    }
    if (indexPath.row == 5) {
        dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        , ^{
            NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
            for (NSString *p in files) {
                NSError *error;
                NSString *path = [cachPath stringByAppendingPathComponent:p];
                if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                }
            }
            [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
    }
    
}

-(void)clearCacheSuccess
{
     _cachLab.text = @"0M";
}

- (void)setFeedbackViewController:(UIViewController *)controller shouldPush:(BOOL)shouldPush{
    [[UMFeedback sharedInstance] setFeedbackViewController:nil shouldPush:YES];
}

@end
